﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;

    public class RibbonsEffect extends MapResourceEffect {

        private var m_isLoading:Boolean = false;
        private var m_isItemImagesLoaded:Boolean = false;
        private var m_itemImageLoadedHash:Object;
        private var m_bitmapDataHash:Object;
        private var m_isAnimationCompleted:Boolean = false;
        private var m_backBitmap:Bitmap;
        private var m_frontBitmap:Bitmap;

        public function RibbonsEffect(_arg1:MapResource){
            super(_arg1);
            this.animate(1);
        }
        override public function animate(_arg1:uint):Boolean{
            if (this.m_isAnimationCompleted){
                return (false);
            };
            if (((isMapResourceLoaded) && (!(this.m_isLoading)))){
                this.loadAnimationEffect();
                return (false);
            };
            return (true);
        }
        override public function cleanUp():void{
            this.m_itemImageLoadedHash = null;
            this.m_bitmapDataHash = null;
            if (this.m_frontBitmap){
                if (this.m_frontBitmap.parent){
                    this.m_frontBitmap.parent.removeChild(this.m_frontBitmap);
                };
                this.m_frontBitmap.bitmapData.dispose();
                this.m_frontBitmap = null;
            };
            if (this.m_backBitmap){
                if (this.m_backBitmap.parent){
                    this.m_backBitmap.parent.removeChild(this.m_backBitmap);
                };
                this.m_backBitmap.bitmapData.dispose();
                this.m_backBitmap = null;
            };
            this.m_isAnimationCompleted = true;
            super.cleanUp();
        }
        override public function reattach():void{
            var _local21:BitmapData;
            var _local24:String;
            var _local25:int;
            if (!this.m_isItemImagesLoaded){
                return;
            };
            var _local1:Vector3 = m_mapResource.getReference().getSize();
            var _local2:IsoRect = IsoRect.getIsoRectFromSize(_local1);
            var _local3:Number = 12;
            var _local4:Number = 10;
            var _local5:Number = (_local3 >> 1);
            var _local6:Number = (_local4 >> 1);
            var _local7:Number = this.m_bitmapDataHash["1SW"].width;
            var _local8:Number = this.m_bitmapDataHash["1SW"].height;
            var _local9:Number = (_local7 - _local3);
            var _local10:Number = (_local8 - _local4);
            var _local11:Number = 0;
            var _local12:Number = (((_local2.height - _local2.bottom.y) + (_local10 >> 1)) - _local4);
            var _local13:Number = _local2.width;
            var _local14:Number = (_local2.height + _local4);
            var _local15:BitmapData = new BitmapData(_local13, _local14, true, 0);
            var _local16:BitmapData = _local15.clone();
            var _local17:Point = new Point();
            var _local18:Number = (_local1.x * IsoRect.TILE_SIZE);
            var _local19:Number = (-(_local1.y) * IsoRect.TILE_SIZE);
            var _local20:Point = new Point();
            var _local22:Number = 0;
            var _local23:Number = 0;
            _local21 = this.m_bitmapDataHash["1SW"];
            _local22 = -(_local5);
            _local23 = (-(_local10) + _local6);
            _local25 = _local1.x;
            if (_local25 > 0){
                while (_local25--) {
                    _local20 = IsoRect.tilePosToPixelPos((((_local1.x - 1) - _local25) * IsoRect.TILE_SIZE), _local19, 0);
                    _local17.x = ((_local20.x + _local11) + _local22);
                    _local17.y = ((_local20.y + _local12) + _local23);
                    _local16.copyPixels(_local21, _local21.rect, _local17, null, null, true);
                };
            };
            _local21 = this.m_bitmapDataHash["1NW"];
            _local22 = 0;
            _local23 = 0;
            _local25 = _local1.y;
            if (_local25 > 0){
                while (_local25--) {
                    _local20 = IsoRect.tilePosToPixelPos(0, (-(((_local1.y - 1) - _local25)) * IsoRect.TILE_SIZE), 0);
                    _local17.x = ((_local20.x + _local11) + _local22);
                    _local17.y = ((_local20.y + _local12) + _local23);
                    _local16.copyPixels(_local21, _local21.rect, _local17, null, null, true);
                };
            };
            _local21 = this.m_bitmapDataHash["1NE"];
            _local22 = 0;
            _local23 = (-(_local10) + _local6);
            _local25 = _local1.x;
            if (_local25 > 0){
                while (_local25--) {
                    _local20 = IsoRect.tilePosToPixelPos((_local25 * IsoRect.TILE_SIZE), 0, 0);
                    _local17.x = ((_local20.x + _local11) + _local22);
                    _local17.y = ((_local20.y + _local12) + _local23);
                    _local15.copyPixels(_local21, _local21.rect, _local17, null, null, true);
                };
            };
            _local21 = this.m_bitmapDataHash["1SE"];
            _local22 = 0;
            _local23 = 0;
            _local25 = _local1.y;
            if (_local25 > 0){
                while (_local25--) {
                    _local20 = IsoRect.tilePosToPixelPos(_local18, (-(((_local1.y - 1) - _local25)) * IsoRect.TILE_SIZE), 0);
                    _local17.x = ((_local20.x + _local11) + _local22);
                    _local17.y = ((_local20.y + _local12) + _local23);
                    _local15.copyPixels(_local21, _local21.rect, _local17, null, null, true);
                };
            };
            var _local26:Sprite = (m_mapResource.getDisplayObject() as Sprite);
            var _local27:Number = (-(m_mapResource.displayObjectOffsetY) / m_mapResource.displayObject.scaleY);
            this.m_frontBitmap = new Bitmap(_local16);
            this.m_frontBitmap.y = (_local27 - this.m_frontBitmap.height);
            _local26.addChild(this.m_frontBitmap);
            this.m_backBitmap = new Bitmap(_local15);
            this.m_backBitmap.y = (_local27 - this.m_backBitmap.height);
            _local26.addChildAt(this.m_backBitmap, _local26.getChildIndex(m_mapResource.content));
        }
        protected function loadAnimationEffect():void{
            var _local2:XML;
            var _local3:ItemImage;
            var _local4:String;
            var _local1:XMLList = Global.gameSettings.getRibbonSetByName("ribbonA");
            this.m_itemImageLoadedHash = {};
            for each (_local2 in _local1) {
                _local4 = _local2.attribute("name");
                this.m_itemImageLoadedHash[_local4] = false;
            };
            this.m_bitmapDataHash = {};
            for each (_local2 in _local1) {
                _local3 = new ItemImage(_local2);
                _local3.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                _local3.load();
            };
            this.m_isLoading = true;
        }
        protected function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:String;
            var _local5:Boolean;
            var _local6:Boolean;
            if (!this.m_isItemImagesLoaded){
                _local2 = (_arg1.target as ItemImage);
                _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                if (this.m_isAnimationCompleted){
                    return;
                };
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                this.m_bitmapDataHash[_local2.name] = (_local3.image as Bitmap).bitmapData;
                _local4 = _local2.name;
                this.m_itemImageLoadedHash[_local4] = true;
                _local5 = true;
                for each (_local6 in this.m_itemImageLoadedHash) {
                    if (!_local6){
                        _local5 = false;
                        break;
                    };
                };
                if (_local5){
                    this.m_isLoading = false;
                    this.m_isItemImagesLoaded = true;
                    this.reattach();
                };
            };
        }

    }
}//package Classes.effects 
